package com.example.gdyh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.gdyh.dao.TPreGrpMapper;
import com.example.gdyh.pojo.Jsons;
import com.example.gdyh.pojo.TPreGrp;
import com.example.gdyh.util.MyException;
import java.io.Serializable;
import java.util.List;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping({"/link"})
public class ClientController {
    @Autowired
    private TPreGrpMapper tPreGrpMapper;
    @Resource
    private RedisTemplate redisTemplate;
   /* @Autowired
   private RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
*/

    @RequestMapping({"/SelectAllLocalhost"})
    public Jsons<Object> SelectAllLocalhost() throws MyException {
        String key="result";
        ListOperations<String,Object> operations=redisTemplate.opsForList();
        if(redisTemplate.hasKey(key)){
            return (Jsons<Object>) operations.range(key,0,1);

        }else {
            List<TPreGrp> tPreGrps = this.tPreGrpMapper.selectListLocal();

            if (tPreGrps.size() != 0) {
                JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(tPreGrps));
                Jsons<Object> jsons = new Jsons();
                jsons.setCode("200");
                jsons.setMessage("查询成功");
                jsons.setResult(jsonArray);
                return jsons;

            }
            throw new MyException("500", "查询错误，请稍后重试");
        }
    }

    @RequestMapping({"/SelectAllGrantClient"})
    public Jsons<Object> SelectAllGrantClient() throws MyException {
        List<TPreGrp> tPreGrps = this.tPreGrpMapper.selectAllGrant();
        JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(tPreGrps));
        if (tPreGrps.size() != 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setMessage("查询成功");
            jsons.setCode("200");
            jsons.setResult(jsonArray);

          /*  rabbitTemplate.convertAndSend("DZR.direct", "dzr", jsons);*/
            return jsons;
        }
        throw new MyException("500", "查询失败"+ jsonArray);


    }

    @RequestMapping({"/SelectAllClient"})
    public Jsons<Object> SelectAllClient() throws MyException {
        List<TPreGrp> tPreGrps = this.tPreGrpMapper.selectAllClient();
        JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(tPreGrps));
        if (tPreGrps.size() != 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setMessage("查询成功");
            jsons.setCode("200");
                    jsons.setResult(jsonArray);
            return jsons;
        }
        throw new MyException("500", "查询失败"+ jsonArray);
    }

    @RequestMapping({"/SelectClientById"})
    public Jsons<Object> SelectClientById(@RequestBody JSONObject jsonObject) throws MyException {
        System.out.println(jsonObject.toJSONString());
        TPreGrp tPreGrp = this.tPreGrpMapper.selectClientById(jsonObject);
        Object o = JSONObject.toJSON(tPreGrp);
        if (tPreGrp != null) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("200");
            jsons.setMessage("查询成功");
                    jsons.setResult(o);
            return jsons;
        }
        throw new MyException("500", "查询错误，请稍后重试");
    }

    @RequestMapping({"/SelectLocalByName"})
    public Jsons<Object> SelectLocalByName(@RequestBody JSONObject jsonObject) throws MyException {
        System.out.println(jsonObject.toJSONString());
        Jsons<Object> jsons = new Jsons();
        List<TPreGrp> tPreGrp = this.tPreGrpMapper.selectLocalByName(jsonObject);
        for (int i = 0; i < tPreGrp.size(); i++) {
            if (((TPreGrp)tPreGrp.get(i)).getPrecustomername() != null) {
                jsons.setCode("查询成功");
                jsons.setMessage("200");
                        jsons.setResult(tPreGrp);
                System.out.println(jsons);
                return jsons;
            }
        }
        throw new MyException("500", "查询错误，请稍后重试");
    }

    @RequestMapping({"/SelectLocalByName1"})
    public Jsons<Object> SelectLocalByName1(@RequestBody JSONObject jsonObject) throws MyException {
        System.out.println(jsonObject.toJSONString());
        TPreGrp tPreGrp = this.tPreGrpMapper.selectLocalByName1(jsonObject);
        JSONObject o = (JSONObject)JSONObject.toJSON(tPreGrp);
        if (tPreGrp.getPrecustomername() != null) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("查询成功");
            jsons.setMessage("200");
                    jsons.setResult(o);
            System.out.println(jsons);
            return jsons;
        }
        throw new MyException("500", "查询错误，请稍后重试");
    }

    @RequestMapping({"/SelectGrantById"})
    public Jsons<Object> SelectGrantById(@RequestBody JSONObject jsonObject) throws MyException {
        TPreGrp tPreGrp = this.tPreGrpMapper.SelectGrantById(jsonObject);
        Object o = JSONObject.toJSON(tPreGrp);
        if (tPreGrp.getCustomerno() != null) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("查询成功");
            jsons.setMessage("200");
                    jsons.setResult(o);
            return jsons;
        }
        throw new MyException("500", "查询错误，请稍后重试");
    }

    @RequestMapping({"/DeleteById"})
    public Jsons<Object> DeleteById(@RequestBody JSONObject jsonObject) throws MyException {
        System.out.println(jsonObject.toJSONString());
        int i = this.tPreGrpMapper.deleteById((Serializable)jsonObject);
        if (i > 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("success");
            jsons.setMessage("200");
                    jsons.setResult(Integer.valueOf(i));
            return jsons;
        }
        throw new MyException("500", "删除错误，请稍后重试");
    }

    @RequestMapping({"/InsertClient"})
    public Jsons<Object> InsertClient(@RequestBody JSONObject jsonObject) throws MyException {
        List<TPreGrp> list = (List<TPreGrp>)jsonObject.get("resu");
        System.out.println(jsonObject.toJSONString());
        int i = this.tPreGrpMapper.insertOneClient(list);
        if (i > 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("200");
            jsons.setMessage("success");
                    jsons.setResult(Integer.valueOf(i));
            return jsons;
        }
        throw new MyException("500", "添加失败");
    }

    @RequestMapping({"/UpdateClientById"})
    public Jsons<Object> UpdateClientById(@RequestBody JSONObject jsonObject) throws MyException {
        System.out.println(jsonObject.toJSONString());
        int i = this.tPreGrpMapper.updateByPrimaryKeySelective(jsonObject);
        if (i > 0) {
            Jsons<Object> jsons = new Jsons();
            jsons.setCode("200");
            jsons.setMessage("修改成功" + i + "条");
                    jsons.setResult(Integer.valueOf(i));
            return jsons;
        }
        throw new MyException("500", "修改失败");
    }
}
